package User;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.sql.SQLException;
import java.util.Objects;

class RegisterDemo extends JFrame {
    public RegisterDemo() {
        // 获取显示屏的大小
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int sw = screenSize.width;
        int sh = screenSize.height;
        // 设置窗口的位置
        int width = 500;
        int height = 600;
        this.setBounds((sw - width) / 2, (sh - height) / 2, width, height);
        this.setTitle("注册界面");

        this.setIconImage(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/pic/chatping.jpg"))); // 图标
        ImageIcon background = new ImageIcon(Objects.requireNonNull(this.getClass().getResource("/pic/chat.jpg"))); // 背景图片


        JLabel label = new JLabel(background); // 把背景图片显示在一个标签里面
        label.setBounds(0, 0, this.getWidth(), this.getHeight()); // 把标签的大小位置设置为图片刚好填充整个面板
        JPanel imagePanel = (JPanel) this.getContentPane(); // 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
        imagePanel.setOpaque(false); // 窗口透明
        this.getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE)); // 把背景图片添加到分层窗格的最底层作为背景

        // 盒子模块
        Box userBox = Box.createHorizontalBox();
        Box pbox = Box.createHorizontalBox();
        Box repbox = Box.createHorizontalBox();
        Box vbox = Box.createVerticalBox();

        // 创建界面工具类
        JLabel userLabel = new JLabel("用户名 :"); // 文本设计
        userLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        userLabel.setForeground(new Color(0x02FFFF)); // 设置前景色
        JTextField userField = new JTextField(); // 输入框
        userField.setColumns(15);

        JLabel pLabel = new JLabel("密    码 :"); // 文本设计
        pLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        pLabel.setForeground(new Color(0x02FFFF)); // 设置前景色
        JPasswordField pFd = new JPasswordField();
        pFd.setColumns(15);
        pFd.setEchoChar('●'); // 星星符号

        JLabel RepLabel = new JLabel("确认密码:"); // 文本设计
        RepLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
        RepLabel.setForeground(new Color(0x02FFFF)); // 设置前景色
        JPasswordField RepFd = new JPasswordField();
        RepFd.setColumns(15);
        RepFd.setEchoChar('●'); // 星星符号

        JButton button1 = new JButton("注册"); // 注册按钮
        JButton button2 = new JButton("重置"); // 重置按钮
        JMenu Menubutton3 = new JMenu("返回登录"); // 返回登录按钮

        // 字体设置
        button1.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button1.setForeground(Color.white); // 设置前景色
        button1.setBackground(new Color(0x08BDFD));
        button1.setBounds((this.getWidth() - 120 - 180) / 2, this.getHeight() - 150, 120, 30); // 设置按钮位置，及按钮大小
        button1.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 鼠标手势的设置

        button2.setFont(new Font("微软雅黑", Font.BOLD, 18));
        button2.setForeground(Color.white); // 设置前景色
        button2.setBackground(new Color(0x08BDFD));
        button2.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 鼠标手势的设置
        button2.setBounds((this.getWidth() - 120 + 180) / 2, this.getHeight() - 150, 120, 30); // 设置按钮位置，及按钮大小

        Menubutton3.setFont(new Font("微软雅黑", Font.BOLD, 12));
        Menubutton3.setForeground(Color.white); // 设置前景色
        Menubutton3.setBackground(new Color(0x08BDFD));
        Menubutton3.setUI(new BasicButtonUI()); // 恢复基本视觉效果
        Menubutton3.setBounds(5, this.getHeight() - 70, 85, 20); // 设置按钮位置，及按钮大小
        Menubutton3.setContentAreaFilled(false); // 设置按钮透明
        Menubutton3.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 分区模块布局
        userBox.add(userLabel);
        userBox.add(Box.createHorizontalStrut(5)); // 插入中间盒子宽度为5，作为相邻文本的空隙
        userBox.add(userField);

        pbox.add(pLabel);
        pbox.add(Box.createHorizontalStrut(5));
        pbox.add(pFd);

        repbox.add(RepLabel);
        repbox.add(Box.createHorizontalStrut(5));
        repbox.add(RepFd);

        // 大盒子
        vbox.add(Box.createVerticalStrut(90)); // 插入中间盒子高度为90，作为上下文本的空隙
        vbox.add(userBox);
        vbox.add(Box.createVerticalStrut(65));
        vbox.add(pbox);
        vbox.add(Box.createVerticalStrut(65));
        vbox.add(repbox);

        // 转换
        JPanel panel = new JPanel();
        panel.setOpaque(false); // 面板透明
        panel.add(vbox, BorderLayout.CENTER); // 将大盒子添加到jLabel盒子里
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        button1.addActionListener(e -> {
            String pField = new String(pFd.getPassword()); // 转换
            String repField = new String(RepFd.getPassword()); // 转换
            try {
                if (userField.getText().isEmpty() || pField.isEmpty() || repField.isEmpty()) {
                    if (userField.getText().isEmpty()) {
                        JOptionPane.showMessageDialog(null, "用户名不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    } else if (pField.isEmpty()) {
                        JOptionPane.showMessageDialog(null, "密码不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null, "确认密码不能为空", "提示", JOptionPane.WARNING_MESSAGE);
                    }
                } else {
                    if (!pField.equals(repField)) {
                        JOptionPane.showMessageDialog(null, "两次密码不一致", "提示", JOptionPane.WARNING_MESSAGE);
                    } else {
                        init(userField, pFd);
                    }
                }

            } catch (Exception exception) {
                JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });
        button2.addActionListener(e -> {
            userField.setText(null);
            pFd.setText(null);
            RepFd.setText(null);
        });
        Menubutton3.addActionListener(e -> {
            try {
                this.dispose();
                Thread.sleep(1000);
                new LoginDemo();
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
            }
        });
        this.add(button1);
        this.add(button2);
        this.add(Menubutton3);
        this.add(panel);
        this.setVisible(true);
        this.setResizable(false);
    }

    public void init(JTextField userField, JPasswordField pFd) {
        Register register;
        UserRegister userRegister;
        try {
            register = new Register();
            register.setUserName(userField.getText());
            char[] p1 = pFd.getPassword();
            register.setPassword(new String(p1));
            userRegister = new UserRegister();
            userRegister.writeRegister(register);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "异常", "警告", JOptionPane.WARNING_MESSAGE);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
